﻿using System;
using System.IO;
using LingDong.ConfigProvider;

namespace LingDong.IndexEntities.Helper
{
    /// <summary>
    /// Get current merged index files name
    /// </summary>
    public class IndexNameHelper
    {
        #region Public Methods

        public static void CreateIndexDirectory()
        {
            string dir = ConfigCommon.Default.LingDongDataRoot + ConfigIndex.Default.IndexDir;
            Directory.CreateDirectory(dir);
        }

        /// <summary>
        /// Return if any one of index files exists
        /// </summary>
        /// <returns></returns>
        public static bool IndexFileAlreadyExists()
        {
            if (File.Exists(GetDictionaryFileName()) ||
                File.Exists(GetInvertedListFileName()) ||
                File.Exists(GetDocumentIndexFileName()) ||
                File.Exists(GetParsedDocumentFileName()) ||
                File.Exists(GetOriginalDocumentFileName()))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Return if all index files exists
        /// </summary>
        /// <returns></returns>
        public static bool AllIndexFileExists()
        {
            if (File.Exists(GetDictionaryFileName()) &&
                File.Exists(GetInvertedListFileName()) &&
                File.Exists(GetDocumentIndexFileName()) &&
                File.Exists(GetParsedDocumentFileName()) &&
                File.Exists(GetOriginalDocumentFileName()))
            {
                return true;
            }
            return false;
        }

        public static string GetDictionaryFileName()
        {
            if (String.IsNullOrEmpty(dictionaryFileName))
            {
                dictionaryFileName = FormatIndexFileName(ConfigIndex.Default.DictionaryPostfix);
            }
            return dictionaryFileName;
        }

        public static string GetInvertedListFileName()
        {
            if (String.IsNullOrEmpty(invertedListFileName))
            {
                invertedListFileName = FormatIndexFileName(ConfigIndex.Default.InvertedListPostfix);
            }
            return invertedListFileName;
        }

        public static string GetDocumentIndexFileName()
        {
            if (String.IsNullOrEmpty(documentIndexFileName))
            {
                documentIndexFileName = FormatIndexFileName(ConfigIndex.Default.DocumentIndexPostfix);
            }
            return documentIndexFileName;
        }

        public static string GetParsedDocumentFileName()
        {
            if (String.IsNullOrEmpty(parsedDocumentListFileName))
            {
                parsedDocumentListFileName = FormatIndexFileName(ConfigIndex.Default.ParsedDocumentPostfix);
            }
            return parsedDocumentListFileName;
        }

        public static string GetOriginalDocumentFileName()
        {
            if (String.IsNullOrEmpty(originalDocumentListFileName))
            {
                originalDocumentListFileName = FormatIndexFileName(ConfigIndex.Default.OriginalDocumentPostfix);
            }
            return originalDocumentListFileName;
        }

        #endregion


        #region Private Methods
        
        private static string FormatIndexFileName(string postfix)
        {
            return String.Format("{0}{1}{2}.{3}",
                ConfigCommon.Default.LingDongDataRoot,
                ConfigIndex.Default.IndexDir,
                ConfigIndex.Default.IndexPrefix,
                postfix);
        }

        #endregion


        #region Private Members

        private static string dictionaryFileName;
        private static string invertedListFileName;
        private static string documentIndexFileName;
        private static string parsedDocumentListFileName;
        private static string originalDocumentListFileName;

        #endregion
    }
}
